import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/index' },
  { path: '/index', component: () => import("@/views/index") },
  { path: '/home', component: () => import("@/views/home"), children:[
          { path: '/login', component: () => import("@/views/login") },
          { path: '/profile', component: () => import("@/views/profile")},
          { path: '/register', component: () => import("@/views/register") },
          { path: '/article', component: () => import("@/views/article") },
          { path: '/article/:id', component: () => import("@/views/articleChild") },
          { path: '/comment', component: () => import("@/views/comment") },
          { path: '/photos', component: () => import("@/views/photos") },
          { path: '/photos/:id', component: () => import("@/views/photosChild") },
          { path: '/amusement', component: () => import("@/views/amusement") },
    ] },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
    if (to.path === '/login' || to.path === '/register' || to.path === '/index') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
        return next('/login')
    }
    next()
})

export default router
